/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package com.alien.admin.modules.deploy.domain;

import lombok.AllArgsConstructor;
import lombok.Data;
import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.ApiModelProperty;
import cn.hutool.core.bean.copier.CopyOptions;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.sql.Timestamp;
import java.io.Serializable;
import java.util.Date;

import  net.sf.log4jdbc.sql.jdbcapi.DriverSpy;
/**
* @website https://el-admin.vip
* @description /
* @author zhouseyi
* @date 2021-09-22
**/
@Entity
@Data
@Table(name="dp_deploy_rec")
@AllArgsConstructor
@NoArgsConstructor
public class DeployRec implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    @ApiModelProperty(value = "id")
    private Long id;

    @Column(name = "env",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "环境")
    private String env;

    @Column(name = "project_id",nullable = false)
    @NotNull
    @ApiModelProperty(value = "项目id")
    private Integer projectId;

    @Column(name = "project_name",nullable = false)
    @NotBlank
    @ApiModelProperty(value = "项目名称")
    private String projectName;

    @Column(name = "stage",nullable = false)
    @NotNull
    @ApiModelProperty(value = "部署阶段")
    private Integer stage;

    @Column(name = "source_branch")
    @ApiModelProperty(value = "源代码分支")
    private String sourceBranch;

    @Column(name = "target_branch")
    @ApiModelProperty(value = "targetBranch")
    private String targetBranch;

    @Column(name = "build_tpl_id",nullable = false)
    @NotNull
    @ApiModelProperty(value = "构建模板id")
    private Integer buildTplId;

    @Column(name = "build_queue_id")
    @ApiModelProperty(value = "构建id")
    private Long buildQueueId;

    @Column(name = "build_param")
    @ApiModelProperty(value = "buildParam")
    private String buildParam;

    @Column(name = "build_log")
    @ApiModelProperty(value = "构建日志")
    private String buildLog;

    @Column(name = "deploy_param")
    @ApiModelProperty(value = "部署参数")
    private String deployParam;

    @Column(name = "deploy_tpl_id",nullable = false)
    @NotNull
    @ApiModelProperty(value = "部署模板id")
    private Integer deployTplId;

    @Column(name = "operator_id")
    @ApiModelProperty(value = "operatorId")
    private Long operatorId;

    @Transient
    private String operatorName;
    @Transient
    private String codeType;

    @Column(name = "deploy_time")
    @ApiModelProperty(value = "deployTime")
    private Timestamp deployTime;

    @Column(name = "deploy_status",nullable = false)
    @NotNull
    @ApiModelProperty(value = "部署状态")
    private Integer deployStatus;

    @Column(name = "jenkins_instance_id",nullable = false)
    @ApiModelProperty(value = "jenkins实例id")
    private Integer jenkinsInstanceId;

    @Column(name = "build_image_url",nullable = false)
    @ApiModelProperty(value = "镜像版本")
    private String buildImageUrl;

    @Column(name = "k8s_instance_id",nullable = false)
    @ApiModelProperty(value = "集群id")
    private Integer k8SInstanceId;

    @Column(name = "name_space",nullable = false)
    @ApiModelProperty(value = "命名空间")
    private String nameSpace;

    @Column(name = "deploy_log",nullable = false)
    @ApiModelProperty(value = "集群日志")
    private String deployLog;

    @Column(name = "rollback_replica_set_name")
    @ApiModelProperty(value = "回滚的副本集名称")
    private String rollbackReplicaSetName;

    @Column(name = "publish_deploy_id")
    @ApiModelProperty(value = "流程部署id")
    private Long publishDeployId;

    @Column(name = "deploy_yml")
    @ApiModelProperty(value = "容器部署yml")
    private String deployYml;

    public void copy(DeployRec source){
        BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
    }

    public DeployRec(Long id, String env, Integer projectId, String projectName, Integer stage, String sourceBranch, Long operatorId, String targetBranch, Date deployTime, Integer deployStatus){
        this.id = id;
        this.env = env;
        this.projectId = projectId;
        this.projectName = projectName;
        this.stage = stage;
        this.sourceBranch = sourceBranch;
        this.operatorId = operatorId;
        this.targetBranch = targetBranch;
        this.deployTime = new Timestamp(deployTime.getTime());
        this.deployStatus = deployStatus;
    }
}
